Skip to content

Conversation

@dogukandemir
Copy link

Prefer resolving target types from DI (if registered) and fall back to ActivatorUtilities, ensuring container registered services like AddHttpClient are honored.

Motivation and Context

Resolving issue #685. Target types were always created by ActivatorUtilities, bypassing DI registrations. This PR respects DI first and fall back to ActivatorUtilities.

How Has This Been Tested?

It's been tested in unit tests. In McpServerBuilderExtensionsCreateTargetHelperTests, a tool is created and BaseAddress of registered HttpClient has been returned from the tool to verify typed HttpClient resolved correctly.

Breaking Changes

There is no breaking behavioral change. This PR will extend CreateTarget capabilities by respecting services DI.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
    • This change is not related with protocol itself.
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

This PR closes #685

@dogukandemir dogukandemir force-pushed the bugfix/respect-di-while-creating-target-object branch from 1d03f2e to bc00585 Compare August 7, 2025 23:31
Comment on lines +793 to +797
var isService = services.GetService<IServiceProviderIsService>()?.IsService(type) == true;

return isService
? services.GetRequiredService(type)
: ActivatorUtilities.CreateInstance(services, type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub and I previously discussed whether or not we should try to resolve [McpServerToolType]'s directly from the DI container before using ActivatorUtilites. This is not something we do for ASP.NET Core MVC Controllers, but it is something we do for SignalR Hubs.

I can't find the conversation right now, but I remember recommending against resolving the tool/resource/prompt type from DI, because resolving SignalR Hubs from DI has been a source of bugs in SignalR in the past, and I hadn't heard people ask for MVC Controllers to get resolved directly from DI either.

I didn't consider the typed AddHttpClient scenario specifically, but MCP handlers are no different than MVC Controllers in this regard. Just as with Controllers, you have to inject the typed client into your MCP handler rather than make the MCP handler a typed client itself.

However, unlike MVC and SignalR, MCP handlers don't have currently have stateful properties like Controller.HttpContext or Hub.Clients.Caller. As it stands, it could be a perf optimization to register your handlers as singletons. The downside would be if we introduced a new handler base type in the future to make things like IMcpServer more discoverable inside handlers.

If we decide to go with this change, I think this could be simplified to the following since we always immediately resolve the service if it exists, and we're not trying to throw if the type is missing.

Suggested change
var isService = services.GetService<IServiceProviderIsService>()?.IsService(type) == true;
return isService
? services.GetRequiredService(type)
: ActivatorUtilities.CreateInstance(services, type);
return services.GetService(type) ?? ActivatorUtilities.CreateInstance(services, type);

GitHub appears to be messing with the diff, but the above should replace lines 793-797.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost forgot we merged #570 which allows you to inject the current IMcpServer into constructors and not just the handler method. If someone were to inject an IMcpServer into their MCP handler type and register it as a singleton, they would run into problems.

@halter73
Copy link
Contributor

halter73 commented Aug 8, 2025

Thanks for your contribution. After some further discussion with the team, we don't think it makes sense to resolve MCP handler types as services at this time.

Registering an MCP handler type as a typed client does not align with the intended usage of typed clients which is to act as an HttpClient wrapper that gets injected into your app. So, in the case of the PetsService example, the PetsService should be the thing that gets injected into your [McpServerToolType].

Typed clients:

  • Provide the same capabilities as named clients without the need to use strings as keys.
  • Provide IntelliSense and compiler help when consuming clients.
  • Provide a single location to configure and interact with a particular HttpClient. For example, a single typed client might be used:
    • For a single backend endpoint.
    • To encapsulate all logic dealing with the endpoint.
  • Work with DI and can be injected where required in the app.

https://learn.microsoft.com/dotnet/core/extensions/httpclient-factory#typed-clients

If you don't want to define an extra type, you can use a named client instead.

Also, given that we merged #570 which allows you to constructor inject IMcpServer, RequestContext<TRequestParams> and IProgress<ProgressNotificationValue> into MCP handler types, this change could allow people to accidently introduce bugs by registering their handler types as singletons.

@halter73 halter73 closed this Aug 8, 2025
@dogukandemir dogukandemir deleted the bugfix/respect-di-while-creating-target-object branch August 8, 2025 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

builder.Services.AddHttpClient<...>() doesnt work with the class marked as [McpServerToolType] directly

2 participants